<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <a-drawer
    :title="!modifyStatus ? '添加' : '修改'"
    :visible="visible"
    @close="cancel"
    :body-style="{ paddingBottom: '80px' }"
    :width="750"
  >
    <!-- 新增和创建表单表单 -->
    <el-form :model="dataForm" :rules="dataRule" v-loading="loading" ref="dataForm" @keyup.enter.native="dataSubmit()"
             label-width="80px" style="padding: 0px 10px;margin: 0px 10px"
    >
      <el-form-item label="机构ID" prop="id">
        <el-input v-model="dataForm.id" placeholder="请输入机构ID" size="small" :disabled="modifyStatus"></el-input>
      </el-form-item>
      <el-form-item label="机构名称" prop="orgname">
        <el-input v-model="dataForm.orgname" placeholder="请输入机构名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="机构备注">
        <el-input v-model="dataForm.memo" placeholder="请输入机构备注" size="small"></el-input>
      </el-form-item>
<!--      <el-form-item label="app" prop="appid">-->

<!--        <el-select v-model="dataForm.appid" placeholder="请选择app" class="width100" filterable clearable>-->
<!--          <el-option-->
<!--            v-for="item in applist"-->
<!--            :key="item.appid"-->
<!--            :value="item.appid"-->
<!--            :label="item.appname">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="type">
        <el-input v-model="dataForm.type" placeholder="请输入类型" size="small"></el-input>
      </el-form-item>
      <el-form-item label="cdk数量" prop="id">
        <el-input v-model="dataForm.cdkcount" placeholder="请输入CDK数量" size="small" ></el-input>
      </el-form-item>
      <el-form-item label="兑换比例" prop="conversionrate">
        <el-input type="number" :min="100" v-model="dataForm.conversionrate" placeholder="兑换比例" size="small" style="width: 200px" >
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="系统抽成">
        <el-input type="number" v-model="dataForm.sysextractionrate" placeholder="请输入系统抽成" size="small" style="width: 200px">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="机构抽成">
        <el-input type="number" v-model="dataForm.extractionrate" placeholder="请输入机构抽成" size="small" style="width: 200px">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
    </el-form>
    <div
      :style="{position: 'absolute',right: 0,bottom: 0,width: '100%', borderTop: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',textAlign: 'right',zIndex: 1,}"
    >
      <el-button @click="cancel()" :style="{ marginRight: '8px' }">取消</el-button>
      <el-button type="primary" @click="submit()">确定</el-button>
    </div>
  </a-drawer>
</template>

<script>
import api from '@/api/api'
import utils from '@/api/utils'

export default {
  components: {},
  data() {
    return {
      modifyStatus: false,
      visible: false,
      selOptions: {},
      loading: false,
      applist:[],

      dataForm: {
        id: 0,
        orgname: '',
        cdkcount: '',
        memo: '',
        type: '',
        appid: '',
        status: '',
        adminid: '',
        adminname: '',
        createtime: '',
        updatetime: '',
        sysextractionrate: '',
        extractionrate: '',
        conversionrate: ''
      },
      dataRule: {
        appid: [
          { required: true, message: 'app名字  为必填项', trigger: 'blur' }
        ],orgname: [
          { required: true, message: '机构名字  为必填项', trigger: 'blur' }
        ],
        memo: [
          { required: true, message: '  为必填项', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '类型  为必填项', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态：0正常  为必填项', trigger: 'blur' }
        ],
        adminid: [
          { required: true, message: '管理员ID  为必填项', trigger: 'blur' }
        ],
        adminname: [
          { required: true, message: '创建人  为必填项', trigger: 'blur' }
        ],
        createtime: [
          { required: true, message: '创建时间  为必填项', trigger: 'blur' }
        ],
        updatetime: [
          { required: true, message: '更新时间  为必填项', trigger: 'blur' }
        ],
        extractionrate: [
          { required: true, message: '抽成比例  为必填项', trigger: 'blur' }
        ],
        conversionrate: [
          { required: true, message: '转换比例  为必填项', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) { // 初始化表单验证规则
      this.dataForm.id = id || undefined
      this.modifyStatus = false
      this.visible = true
      utils.getAppList().then(res => {this.applist=res.datalist});
      this.selOptions = this.$constants.selOptions
      this.$nextTick(() => {
        // this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.modifyStatus = true
          api.post('rest/v1/orginfo/info', { id: this.dataForm.id }).then(res => {
            this.dataForm = res
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    // 表单数据提交
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          let postData = Object.assign({}, this.dataForm)
          if (!this.modifyStatus) {
            postData.id == undefined
            api.post('rest/v1/orginfo/save', postData).then(res => {
              this.$message.success('创建成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          } else {
            api.post('rest/v1/orginfo/update', postData).then(res => {
              this.$message.success('操作成功')
              this.reset()
              this.visible = false
              this.$parent.getDataList()
            }).finally(() => {
              this.loading = false
            })
          }
        }
      })
    },
    cancel() {
      this.visible = false
      this.reset()
    },
    reset() {
      this.$refs.dataForm.resetFields()
      this.$data.dataForm = JSON.parse(JSON.stringify(this.$options.data().dataForm))
      //图片回写
      //this.$refs["childData"].fileList=[]
    }
  }
}
</script>
